package com.guoxin.concurrentprogramming.chapter2;

/**
 * @author guoxin
 * @date 2019/7/15
 */
public class VolatileAtomicity {

    private static volatile int i = 0;

    static Object obj = new Object();
    private static class PlusTask extends Thread {
        @Override
        public void run() {

            for (int k = 0; k < 10000; k++) {
                synchronized (obj) {
                    i = i + 1;
                }
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new PlusTask());
            threads[i].start();
        }

        for (Thread t : threads) {
            t.join();
        }

        System.out.println(i);

    }
}
